﻿using DFGK.IRepository;
using DFGK_WMS.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Repository
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
    {
        private readonly DFGK_DB db;
        public BaseRepository(DFGK_DB db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public void AddEntity(TEntity obj)
        {
            db.Set<TEntity>().Add(obj);
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="list"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void AddEntityList(List<TEntity> list)
        {
            db.Set<TEntity>().AddRange(list);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void DelEntity(TEntity obj)
        {
            db.Set<TEntity>().Remove(obj);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void DelEntityList(List<TEntity> list)
        {
            db.Set<TEntity>().RemoveRange(list);
        }

        /// <summary>
        /// 获取全部信息
        /// </summary>
        /// <returns></returns>
        public List<TEntity> GetAllEntityList()
        {
            return db.Set<TEntity>().ToList();
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<TEntity> GetEntityAll()
        {
            return db.Set<TEntity>();
        }

        /// <summary>
        /// 根据条件获取全部数据
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<TEntity> GetEntityWhereAll(Expression<Func<TEntity, bool>> where)
        {
            return db.Set<TEntity>().Where(where);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="OrderField"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="toalCount"></param>
        /// <param name="orderField"></param>
        /// <param name="expression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<TEntity> GetEntityAll<OrderField>(int pageIndex, int pageSize, out int totalCount, Expression<Func<TEntity, OrderField>> orderField, Expression<Func<TEntity, bool>> expression = null, bool isAsc = true)
        {
            IQueryable<TEntity> query = db.Set<TEntity>().AsNoTracking();
            if (expression != null)
            {
                query = query.Where(expression);
            }
            totalCount = query.Count();
            // 排序
            if (isAsc)
                // query.OrderBy(t => t.Id);
                query = query.OrderBy(orderField);
            else
                query = query.OrderByDescending(orderField);
            // 分页
            query = query.Skip(pageSize * (pageIndex - 1))   // 跳过n条
                .Take(pageSize);  // 取前n条

            return query;
        }

        /// <summary>
        /// 无条件获取所有信息
        /// </summary>
        /// <typeparam name="OrderField"></typeparam>
        /// <param name="list"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderField"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<TEntity> GetEntityAlls<OrderField>(IQueryable<TEntity> list, int pageIndex, int pageSize,out int totalCount, Expression<Func<TEntity, OrderField>> orderField, bool isAsc = true)
        {
            totalCount=list.Count();
            // 排序
            if (isAsc)
                list = list.OrderBy(orderField);
            else
                list = list.OrderByDescending(orderField);
            // 分页
            list = list.Skip(pageSize * (pageIndex - 1))   // 跳过n条
                .Take(pageSize);  // 取前n条
            return list;
        }

        /// <summary>
        /// 去除跟踪状态获取数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<TEntity> GetEntityAlls()
        {
            return db.Set<TEntity>().AsNoTracking();
        }

        /// <summary>
        /// 根据主键获取对象信息：long类型
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TEntity? QueryEntityById(long Id)
        {
            return db.Set<TEntity>().Find(Id);
        }

        /// <summary>
        /// 根据条件获取对象信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TEntity? QueryEntityWhere(Expression<Func<TEntity, bool>> where)
        {
            return db.Set<TEntity>().FirstOrDefault(where);
        }
        /// <summary>
        /// 异步获取单个对象(通过条件)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> where)
        {
            return await db.Set<TEntity>().Where(where).FirstOrDefaultAsync();
        }
        /// <summary>
        /// 去除跟踪状态判断信息是否存在
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public TEntity? QueryIsEntityWhere(Expression<Func<TEntity, bool>> where)
        {
            return db.Set<TEntity>().AsNoTracking().FirstOrDefault(where);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        public void UpdEntity(TEntity obj)
        {
            db.Set<TEntity>().Update(obj);
        }
        /// <summary>
        /// 异步修改
        /// </summary>
        /// <param name="eneity">实体</param>
        /// <returns></returns>
        public async Task UpdateAsync(TEntity eneity)
        {
            db.Set<TEntity>().Update(eneity);
            
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="list"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdEntityList(List<TEntity> list)
        {
            db.Set<TEntity>().UpdateRange(list);
        }
    }
}
